package com.example.asus.homework11.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.adapter.CollectionShopAdapter;
import com.example.asus.homework11.bean.CollectListBean;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;


public class CollectionShopFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<CollectListBean> beans;
    private CollectionShopAdapter adapter;
    private int userid;
    private SharedPreferences sharedPreferences;

    public CollectionShopFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collection_shop, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData(view);
        initEvent(view);
    }

    public void initView(View v) {
        adapter = new CollectionShopAdapter();
        recyclerView = v.findViewById(R.id.rv_collectionshop);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    BaseListner<List<CollectListBean>> listner = new BaseListner<List<CollectListBean>>() {
        @Override
        public void onResponse(List<CollectListBean> collectListBeans) {
            beans=collectListBeans;
            if (beans==null)
                Toast.makeText(getContext(),"Kong",Toast.LENGTH_LONG).show();
            else{
                adapter.setData(beans);

            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
        }
    };
    private void initData(View v){
        sharedPreferences = getActivity().getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        BaseModel<List<CollectListBean>> model = new BaseModel<>();
        model.callEnqueue(model.service.getCollectList(userid,0),listner);

    }

    private void initEvent(View v){

    }

}
